/******************************************************************************
 * Copyright (C) 2025 EternalChip, Inc.(Gmbh) or its affiliates.
 * 
 * All Rights Reserved.
 * 
 * @file bsp_led.c
 * 
 * @par dependencies 
 * - bsp_led.h
 * 
 * 
 * @author zy 
 * 
 * @brief Provide the HAL APIs of led and corresponding opetions.
 * 
 * Processing flow:
 * 
 * call directly.
 * 
 * @version V1.0 2025-4-20
 *
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/

#include "bsp_led.h"

//*****************************Thread_Func **********************************//
/*led任务句柄                                                                */
osThreadId_t              led_TaskHandle;

/*led线程句柄                                                                */
const osThreadAttr_t led_Task_attributes = {
  .name = "led_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
//*****************************Thread_Func **********************************//

//****************************Queue_Handler ********************************//
QueueHandle_t led_queue;

//****************************Queue_Handler ********************************//



/**
 * @brief 执行LED的打开、关闭、闪烁操作
 *  
 * @param[in] led_operation_t         //LED操作
 * 
 * @return led_status_t               //LED状态
 * 
 * */
led_status_t led_on_off(led_operation_t led_operation)
{
    led_status_t ret = LED_OK;        //LED状态状态初始化
    
    if ( LED_ON          == led_operation )
    {
        //1.点亮
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); 
    }
    
    if ( LED_OFF         == led_operation )
    {
        //2.熄灭
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    }
    
    if ( LED_TOGGLE      == led_operation )
    {
        //3.闪烁
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    }
    if(LED_BLINL_3_TIMES == led_operation)
    {
        //4.闪烁三次
        for(int i = 0; i < 6; i++)
        {
            HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);
            vTaskDelay(301);
        }
       

    }
    return ret;
}


/**
 * @brief LED任务队列创建、消息接收
 * 
 * @param[in] void *
 * 
 * @return 无
 * 
 * */
void led_task_func(void *argument)
{
    
  led_status_t      led_ret          =          LED_OK;     //LED状态状态初始化
  led_operation_t   led_value        =          LED_ON;     //LED操作状态初始化
  led_queue = xQueueCreate( 10, sizeof( led_operation_t ) );
  if (NULL == led_queue )
  {
      printf("led_queue created failed \r\n");
  } 
  else
  {
      printf("led_queue created successfully \r\n");
  }
  for(;;)
  {
    //printf("Hello led thread\r\n");                       //防止饿死
    
    if( led_queue != 0 )                                  //任务队列不为空
	{
		//并且队列接收消息成功
		if(pdTRUE == xQueueReceive(           led_queue           , 
                                           &( led_value )         ,
                                            ( TickType_t ) 100 ) )
		{
			//打印收到的消息，也就是（led_value）LED操作状态，这取决于led_on_off函数
            printf("received led_queue value = [%d] at time [%d]\r\n", led_value
                                                        ,HAL_GetTick());
            led_ret = led_on_off(led_value);            //LED状态设置
            if( LED_OK == led_ret )                     //收到的消息是（LED_OK）
            {
                printf("led_on_off ok at time [%d]\r\n",HAL_GetTick());
            }
		}
	}
    
    osDelay(100);
  }

}
  /* USER CODE END 5 */
